﻿using Common;
using Model;
using Model.CommonEntities;
using Model.StrategyEntities;
using StockStrategyView.StockTask;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;

namespace StockStrategyView
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            Init();
        }
        //static string ShowTxt = string.Empty;

        #region 初始化界面
        /// <summary>
        /// 界面初始化
        /// </summary>
        private void Init()
        {
            this.cmBox_code.SelectedIndex = 0;
            this.txt_period.Text = "100";
            this.cmBox_type.SelectedIndex = 0;
            this.cmBox_Strategy.SelectedIndex = 0;

            this.cmBox_code2.SelectedIndex = 0;
            this.txt_period2.Text = "100";
            this.cmBox_type2.SelectedIndex = 0;
            this.cmBox_Strategy2.SelectedIndex = 0;
            ///初始化策略比较
            this.cmBox_Compare_code.SelectedIndex = 0;
            this.txt_Compare_period.Text = "100";
            this.cmBox_Compare_type.SelectedIndex = 0;
            
            //AutoScaleMode = AutoScaleMode.None;

            for (int i = 0; i < ckBox_Strategy.Items.Count; i++)
            {
                ckBox_Strategy.SetItemChecked(i, true);
            }

        }
        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            
            string code = txt_stockcode.Text == string.Empty ? cmBox_code.Text == string.Empty ? null : cmBox_code.Text : txt_stockcode.Text;
            int period = txt_period.Text == string.Empty ? 100 : int.Parse(txt_period.Text);
            string code2 = txt_stockcode2.Text == string.Empty ? cmBox_code2.Text == string.Empty ? null : cmBox_code2.Text : txt_stockcode2.Text;
            int period2 = txt_period2.Text == string.Empty ? 100 : int.Parse(txt_period2.Text);

            //TaskBBI.GetData(code, ".XSHE", period, "1d", null);
            //BLJJResult BLJJdata = TaskBLJJ.GetData(code, ".XSHE", period, "1d", null);
            //BLJJResult BLJJdata1 = TaskBLJJ.GetData(code, ".XSHE", period, "1w", null);
            //var DrawDatatest = Common.CalRevenue.CrossCondition.PeriodCross(BLJJdata.TList, BLJJdata.LongList, BLJJdata.ShortList, BLJJdata1.TList, BLJJdata1.LongList, BLJJdata1.ShortList, BLJJdata.BenchmarkList, BLJJdata.CloseList);
            //TotalResult DrawData1 = ChooseStrategy(cmBox_Strategy.Text, code, ".XSHE", period, cmBox_type.Text, null);



            //第一个图表的数据
            TotalResult DrawData = ChooseStrategy(cmBox_Strategy.Text, code, ".XSHE", period, cmBox_type.Text, null);
            //第二个图表的数据
            TotalResult DrawData2 = ChooseStrategy(cmBox_Strategy2.Text, code2, ".XSHE", period2, cmBox_type2.Text, null);
            //DrawChart(DrawData);
            Common.DrawPicture.ChartFunction.DrawChart(chart1, DrawData);
            Common.DrawPicture.ChartFunction.DrawChart(chart2, DrawData2);
            //ShowTxt = DrawData.TradeInfo;
            //ShowTxt += DrawData2.TradeInfo;
            txtBox_Info.Text = DrawData.TradeInfo + DrawData2.TradeInfo;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="Strategy"></param>
        /// <param name="code"></param>
        /// <param name="exchange"></param>
        /// <param name="period"></param>
        /// <param name="type"></param>
        /// <param name="date"></param>
        /// <returns></returns>
        public TotalResult ChooseStrategy(string Strategy, string code = null, string exchange = null, int period = -1, string type = null, string date = null)
        {
            TotalResult DrawData = new TotalResult();
            switch (Strategy)
            {
                case "捕捞季节":
                    BLJJResult BLJJdata = TaskBLJJ.GetData(code, exchange, period, type, null);
                    DrawData = BLL.CalRevenue.Cross.SampleCross(BLJJdata.TList, BLJJdata.LongList, BLJJdata.ShortList, BLJJdata.BenchmarkList, BLJJdata.CloseList);
                    break;
                case "智能辅助线":
                    ZNFZXResult ZNFZXdata = TaskZNFZX.GetData(code, exchange, period, type, null);
                    DrawData = BLL.CalRevenue.Cross.SampleCross(ZNFZXdata.TList, ZNFZXdata.LongList, ZNFZXdata.ShortList, ZNFZXdata.BenchmarkList, ZNFZXdata.CloseList);
                    break;
                case "均线":
                    SMACrossResult SMACrossdata = TaskSMACross.GetData(code, ".XSHE", period, "1d", null, 5, 10);
                    DrawData = BLL.CalRevenue.Cross.SampleCross(SMACrossdata.TList, SMACrossdata.LongList, SMACrossdata.ShortList, SMACrossdata.BenchmarkList, SMACrossdata.CloseList);
                    break;
                case "日+周捕捞":
                    BLJJResult BLJJdata_d = TaskBLJJ.GetData(code, ".XSHE", period, "1d", null);
                    BLJJResult BLJJdata_w = TaskBLJJ.GetData(code, ".XSHE", period / 3, "1w", null);
                    DrawData = BLL.CalRevenue.CrossCondition.DiffPeriodCross(BLJJdata_d.TList, BLJJdata_d.LongList, BLJJdata_d.ShortList, BLJJdata_w.TList, BLJJdata_w.LongList, BLJJdata_w.ShortList, BLJJdata_d.BenchmarkList, BLJJdata_d.CloseList);
                    break;
                case "捕捞+辅助":
                    BLJJResult 捕捞_辅助_bl = TaskBLJJ.GetData(code, ".XSHE", period, "1d", null);
                    ZNFZXResult 捕捞_辅助_fz = TaskZNFZX.GetData(code, ".XSHE", period, "1d", null);
                    DrawData = BLL.CalRevenue.CrossCondition.DiffPeriodCross(捕捞_辅助_bl.TList, 捕捞_辅助_bl.LongList, 捕捞_辅助_bl.ShortList, 捕捞_辅助_fz.TList, 捕捞_辅助_fz.LongList, 捕捞_辅助_fz.ShortList, 捕捞_辅助_bl.BenchmarkList, 捕捞_辅助_bl.CloseList);
                    break;
                case "BBI":
                    BBIResult BBIdata = TaskBBI.GetData(code, ".XSHE", period, "1d", null);
                    DrawData = BLL.CalRevenue.Cross.SampleCross(BBIdata.TList, BBIdata.LongList, BBIdata.ShortList, BBIdata.BenchmarkList, BBIdata.CloseList);
                    break;
                case "捕捞+BBI":
                    BBIResult 捕捞_BBI_bbi = TaskBBI.GetData(code, ".XSHE", period, "1d", null);
                    BLJJResult 捕捞_BBI_bl = TaskBLJJ.GetData(code, ".XSHE", period, "1d", null);
                    DrawData = BLL.CalRevenue.CrossCondition.DiffPeriodCross(捕捞_BBI_bl.TList, 捕捞_BBI_bl.LongList, 捕捞_BBI_bl.ShortList, 捕捞_BBI_bbi.TList, 捕捞_BBI_bbi.LongList, 捕捞_BBI_bbi.ShortList, 捕捞_BBI_bl.BenchmarkList, 捕捞_BBI_bl.CloseList);
                    break;
                case "线性回归":
                    LinearRegressionResult LinearRegressiondata = TaskLinearRegression.GetData(code, ".XSHE", period, "1d", null);
                    DrawData = new TotalResult();//BLL.CalRevenue.Cross.SampleCross(LinearRegressiondata.TList, LinearRegressiondata.LongSlopeList, LinearRegressiondata.ShortSlopeList, LinearRegressiondata.BenchmarkList, LinearRegressiondata.CloseList);
                    break;
                default:
                    DrawData = null;
                    break;
            }
            return DrawData;
        }



        private void button2_Click(object sender, EventArgs e)
        {
            #region
            string str = "38.8,39.97,38.8,40.4,40.4,38.65,38.6866,38.722468,38.75761864,38.8273138944,38.951675060736,39.18594105587712,39.401465771406954,41.88,41.802400000000006,41.604304000000006,41.13604576000001,40.28436209920001,39.50081313126401,38.77994808076289,38.11675223430186,30.49,30.6344,30.993024,31.52924256,32.0332880064,32.701024965888,33.476922469299204,34.3196917729833,35.33053492476564,36.418449336803135,37.94512845617857,39.55610276494286,46.0,45.8766,45.59393600000001,36.6,36.6,36.7736,37.134656,37.481269759999996,45.8,45.6158,45.245168,44.60645792,43.6539412864,42.777625983488,41.6528633851392,40.427319778922495,39.096295009873344,37.951613708491074,36.6457555151325,35.30551952240865,34.20652600837509,33.30535132686757,32.56638808803141,32.49,31.752000000000002,31.137600000000003,30.64608,28.68,28.7156,28.806976,28.94395744,31.44,31.44,31.386000000000003,31.333080000000002,31.281218400000004,31.230394032000003,31.180586151360004,31.131774428332804,28.74,28.7948,28.927007999999997,32.1,31.992600000000003,31.887348000000003,26.73,26.8324,26.932752,27.03109696,27.2806530816,27.520226958336,27.75021788000256,27.971009164802457,28.182968798210357,28.386450046281944,28.581792044430667,28.769320362653442,28.949347548147305,33.27,33.177800000000005,33.087444000000005,32.99889512000001,32.809339315200006,32.627365742592005,32.452671112888325,32.28496426837279,32.12396569763788,31.969407069732362,31.82103078694307,28.26,28.372200000000003,28.724912000000003,29.260417280000002,29.7637922432,30.236964708608,30.88200753191936,31.475446929365813,32.021411175016546,32.684270057514894,33.49615765061311,34.21061873253954,35.144332109984,35.94732561458624,36.637900028544166,36.78,40.88,40.797200000000004,36.74,36.836600000000004,36.931268,37.142017280000005,37.344336588800005,37.538563125248004,37.72502060023808,38.0523193642238,43.18,43.0482,42.774272,42.511301120000006,42.258849075200004,42.016495112192004,41.78383530770432,41.412805189242064,41.06403687788754,40.736194665214285,40.428022985301425,40.13834160618334,39.866041109812336,39.610078643223595,39.36947392463018,39.05991601065976,38.604924409593785,38.07633348044253,37.46364679318057,36.56546330627168,35.81098917726821,35.1772309089053,34.64487396348045,34.19769412932358,33.822063068631806,33.42209171627808,33.094115207348025,32.82517447002538,32.76,30.89,31.01";
            #endregion

            List<decimal> longlist = str.Split(',').ToList().Select<string, decimal>(x => Convert.ToDecimal(x)).ToList();
            BLJJResult BLJJ = TaskBLJJ.GetData("002153", ".XSHE", 159, "1d", null);
            List<decimal> shortlist = BLJJ.CloseList;
            TotalResult DrawData = BLL.CalRevenue.CrossCondition.DiffPeriodCross(BLJJ.TList, BLJJ.LongList, BLJJ.ShortList, BLJJ.TList, longlist, shortlist, BLJJ.BenchmarkList, BLJJ.CloseList);




            //TotalResult data = new TotalResult();
            
            //panel1.Controls.Add(CreatChart(12,0));
            //panel1.Controls.Add(CreatChart(12,200));
            //panel1.Controls.Add(CreatChart(12, 300));
            //panel1.Controls.Add(CreatChart(12, 800));
            //MessageBox.Show(ShowTxt);
            //MessageBox.Show(txtBox_Info.Text);
        }


        #region 策略比较模块
        /// <summary>
        /// 策略比较功能
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripStratrgyCompare_Click(object sender, EventArgs e)
        {
            string code = cmBox_Compare_code.Text == string.Empty ? cmBox_Compare_code.Text == string.Empty ? null : cmBox_Compare_code.Text : cmBox_Compare_code.Text;
            int period = txt_Compare_period.Text == string.Empty ? 100 : int.Parse(txt_Compare_period.Text);
            string code2 = txt_stockcode2.Text == string.Empty ? cmBox_code2.Text == string.Empty ? null : cmBox_code2.Text : txt_stockcode2.Text;
            int period2 = txt_period2.Text == string.Empty ? 100 : int.Parse(txt_period2.Text);

            List<string> result = new List<string>();
            System.Collections.IEnumerator myEnumerator = ckBox_Strategy.CheckedIndices.GetEnumerator();
            int index;

            while (myEnumerator.MoveNext())
            {
                index = (int)myEnumerator.Current;
                ckBox_Strategy.SelectedItem = ckBox_Strategy.Items[index];
                result.Add(ckBox_Strategy.Text);
            }

            for (int i = 0; i < result.Count; i++)
            {
                TotalResult DrawData = ChooseStrategy(result[i], code, ".XSHE", period, cmBox_Compare_type.Text, null);
                if (DrawData != null)
                {
                    panel1.Controls.Add(Common.DrawPicture.ChartFunction.DynamicChart(result[i], code, 0, i * 200, DrawData));
                }
                
            }

            //TotalResult data = new TotalResult();

            //Common.DrawPicture.ChartFunction.DynamicChart(12, 0,data);
            //panel1.Controls.Add(Common.DrawPicture.ChartFunction.DynamicChart(0, 0, data));
            //panel1.Controls.Add(Common.DrawPicture.ChartFunction.DynamicChart(0, 200, data));
            //panel1.Controls.Add(Common.DrawPicture.ChartFunction.DynamicChart(0, 400, data));
            //panel1.Controls.Add(Common.DrawPicture.ChartFunction.DynamicChart(0, 600, data));
        }

        /// <summary>
        /// 清空策略比较结果
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripPanelClear_Click(object sender, EventArgs e)
        {
            panel1.Controls.Clear();
        }
        #endregion

        #region 模拟交易模块
        private void ToolStripSimulatedTrading_Click(object sender, EventArgs e)
        {
            string code = txt_stockcode.Text == string.Empty ? cmBox_code.Text == string.Empty ? null : cmBox_code.Text : txt_stockcode.Text;
            int period = txt_period.Text == string.Empty ? 100 : int.Parse(txt_period.Text);
            string code2 = txt_stockcode2.Text == string.Empty ? cmBox_code2.Text == string.Empty ? null : cmBox_code2.Text : txt_stockcode2.Text;
            int period2 = txt_period2.Text == string.Empty ? 100 : int.Parse(txt_period2.Text);
            

            //第一个图表的数据
            TotalResult DrawData = ChooseStrategy(cmBox_Strategy.Text, code, ".XSHE", period, cmBox_type.Text, null);
            //第二个图表的数据
            TotalResult DrawData2 = ChooseStrategy(cmBox_Strategy2.Text, code2, ".XSHE", period2, cmBox_type2.Text, null);
            //DrawChart(DrawData);
            Common.DrawPicture.ChartFunction.DrawChart(chart1, DrawData);
            Common.DrawPicture.ChartFunction.DrawChart(chart2, DrawData2);
            //ShowTxt = DrawData.TradeInfo;
            //ShowTxt += DrawData2.TradeInfo;
            txtBox_Info.Text = DrawData.TradeInfo + DrawData2.TradeInfo;
        }

        /// <summary>
        /// 显示详细交易信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ToolStripTradeInfo_Click(object sender, EventArgs e)
        {
            MessageBox.Show(txtBox_Info.Text);
        }
        #endregion


    }
}
